﻿using System;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Gmmy.Utility;
using Infrastructure.Repository;
using Infrastructure.Repository.DataContext;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Surging.Core.CPlatform.Utilities;
namespace Host
{
    public class Startup
    {
        public IConfiguration Configuration { get; }

        public Startup(IConfigurationBuilder config)
        {

        }


        public IContainer ConfigureServices(ContainerBuilder builder)
        {
            var services = new ServiceCollection();
            //注入ef上下文
            services.AddDbContext<EfContext>(option =>
            {
                option.UseSqlServer(GlobalSetting.EntityFrameworkConnectionString);
            });
            services.AddCap(x =>
            {
                x.UseDashboard();
                x.UseEntityFramework<EfContext>();
                x.UseRabbitMQ(Environment.GetEnvironmentVariable("EventBusConnection") ?? "127.0.0.1");
            });
            //注入ef工厂
            builder.DataBaseFactoryHostBuilder();
            ConfigureLogging(services);
            builder.Populate(services);
            ServiceLocator.Current = builder.Build();
            return ServiceLocator.Current;
        }

        public void Configure(IContainer app)
        {
            app.Resolve<ILoggerFactory>()
                .AddConsole((c, l) => (int)l >= 3);
        }

        #region 私有方法
        /// <summary>
        /// 配置日志服务
        /// </summary>
        /// <param name="services"></param>
        private void ConfigureLogging(IServiceCollection services)
        {
            services.AddLogging();
        }

        #endregion
    }
}
